package teacher;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tern.data.Entity;
import com.tern.data.Record;
import com.tern.db.DBCommand;
import com.tern.db.SqlHelper;
import com.tern.util.Convert;
import com.tern.util.Trace;
import com.tern.web.PageForm;
import com.tern.web.html;

public class StudentTextExcelPage  extends PageForm 
{
	private Entity entity;
	private int classid;
	public int getClassid() {
		return classid;
	}
	private int teacherID;
	public int getTeacherID() {
		return teacherID;
	}
	//////////////
	private String classCode;
	public String getClassCode() {
		return classCode;
	}
	private String startTimeString;
	public String getStartTimeString() {
		return startTimeString;
	}
	private String course;
	public String getCourse() {
		return course;
	}
	private String teacher;
	public String getTeacher() {
		return teacher;
	}
	
	private Map<Integer,String> testMap;
	public Map<Integer, String> getTestMap() {
		return testMap;
	}
	
	private List<Integer> testIDs;
	public List<Integer> getTestIDs() {
		return testIDs;
	}
	//////////////////
	private String classSelection;
	public String getClassSelection() {
		if(classSelection==null){
			StringBuffer sel=new StringBuffer();
			classSelection=TeacherCommon.createSelection(teacherID,classid,sel);
			String[] selsStrings=sel.toString().split("--");
			classCode=selsStrings[0];
			course=selsStrings[1];
			startTimeString=selsStrings[2];
		}
		return classSelection;
	}
	
	final protected boolean onLoad() throws Exception
	{
		//entity = Entity.createFromSchema("curricula");
		entity=new Entity();
		
		classid = Convert.parseInt(request.getParameter("classid"));
		
		
		try{
		teacherID = Convert.parseInt(session.getAttribute("userID").toString());
		teacher=session.getAttribute("userName").toString();
		}catch (Exception e) {
			
		}
		if(teacherID==0){
			teacherID = Convert.parseInt(request.getParameter("tid"));
			teacher=new String(request.getParameter("teacher").getBytes("iso-8859-1") , "GBK");
			classCode= new String(request.getParameter("classCode").getBytes("iso-8859-1") , "GBK");
			course= new String(request.getParameter("course").getBytes("iso-8859-1") , "GBK");
			startTimeString= new String(request.getParameter("startTimeString").getBytes("iso-8859-1") , "GBK");
		}
		retriveEntity();
		return true;
	}
	private void retriveEntity() {
		DBCommand cmd=SqlHelper.helper().createCommand();
		try {
			if(classid<=0){
				String startTime = com.tern.util.Configuration.getInstance()
				.getValue("class.startTime");
				
				Entity classEntity=Entity.createFromSchema("class");
				classEntity.retriveFromDB("teacherID="+teacherID+" AND startTime='"+startTime+"'");
				if(classEntity.getRowsCount()>0){
					classid=classEntity.get(0).getInt("classID");
				}else{
					classEntity.retriveFromDB("teacherID="+teacherID+" order by classID desc");
					if(classEntity.getRowsCount()>0)
					classid=classEntity.get(0).getInt("classID");
				}
			}
			
			Entity testEntity=Entity.createFromSchema("test");
			testEntity.retriveFromDB("classID="+classid);
			
			
			entity.addColumn("curriculaID", "N", true, false, null);
			entity.addColumn("stuID", "N", true, false, null);
			entity.addColumn("stuNum", "S:0,50", true, false, null);
			entity.addColumn("stuName", "S:0,50", true, false, null);
			entity.addColumn("total", "N:6,2", true, false, null);
			
			testMap=new HashMap<Integer,String>();
			testIDs=new ArrayList<Integer>();
			for(int i=0;i<testEntity.getRowsCount();i++){
				testIDs.add(Convert.parseInt(testEntity.get(i).getOriValue("testID")));
				testMap.put(Convert.parseInt(testEntity.get(i).getOriValue("testID")),testEntity.get(i).getString("testName"));
				entity.addColumn(testEntity.get(i).getString("testID"), "N:6,2", true, false, null);
			}
			
			Entity cureEntity=Entity.createFromSchema("curricula");
			cureEntity.retriveFromDB("classID="+classid+" order by stuID");
			
			Map<Integer, String> stuMap=new HashMap<Integer, String>();
			String stuSqlString="select stuID,stuNum from students";
			cmd.setCommand(stuSqlString);
			ResultSet rstu=cmd.executeResultset();
			while (rstu.next()) {
				stuMap.put(rstu.getInt("stuID"), rstu.getString("stuNum"));
			}
			
			for(int i=0;i<cureEntity.getRowsCount();i++){
				Record row=entity.createRow();
				row.set("stuID", cureEntity.get(i).getOriValue("stuID"));
				row.set("stuNum",stuMap.get(Convert.parseInt(cureEntity.get(i).getOriValue("stuID"))));
				row.set("stuName", cureEntity.get(i).getString("stuID"));
				row.set("total", 0);
				for(int i1=0;i1<testEntity.getRowsCount();i1++){
				row.set(testEntity.get(i1).getString("testID"), 0);
				}
				entity.addRow(row);
			}
			
			
			String sqlString="select c.curriculaID,a.stuID,b.stuNum,b.stuName,a.teacherSourseScore,a.paperScore,a.testID from submit a,students b,curricula c where" +
					" a.stuID=b.stuID and c.stuID=a.stuID and testID in (select testID from test where classID="+classid+") order by stuID";
			cmd.setCommand(sqlString);
			ResultSet rs=cmd.executeResultset();
			
			
			int preStu=-1;
			int i=0;
			while(rs.next()){
				entity.get(i).set("curriculaID", rs.getString("curriculaID"));
//				entity.get(i).set("stuNum", rs.getString("stuNum"));
//				entity.get(i).set("stuName", rs.getString("stuName"));
				entity.get(i).set(rs.getString("testID"),rs.getDouble("teacherSourseScore")+rs.getDouble("paperScore"));
				entity.get(i).set("total", entity.get(i).getDouble("total")+rs.getDouble("teacherSourseScore")+rs.getDouble("paperScore"));
				if(preStu==-1){
					preStu=rs.getInt("stuID");
				}
				
				if(rs.getInt("stuID")!=preStu){
					i++;
				}
				preStu=rs.getInt("stuID");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Trace.out(Trace.Error, "StudentTestPage", e);
		}finally{
			cmd.Dispose();
		}
	}
	public Entity getCurricula()
	{		
		return entity;
	}
	protected boolean onEvent(String eventName, String eventParams) {
		return true;
	}
}
